package stack;

import java.util.Arrays;
import java.util.Stack;

public class MyStack implements IStack {
    private int[] arr;
    private int usedSize;
    public static final int DEFAULT_CAPACITY = 10;

    MyStack() {
        arr = new int[DEFAULT_CAPACITY];
    }

    @Override
    public void push(int val) {
        if (full()) {
            arr = Arrays.copyOf(arr, arr.length * 2);
        }
        arr[usedSize++] = val;
    }

    @Override
    public int pop() {
        if (empty()) {
            throw new EmptyException("栈空,无法出栈");
        }
        usedSize--;
        return arr[usedSize];
    }

    @Override
    public int peek() {
        if (empty()) {
            throw new EmptyException("栈空,无法出栈");
        }
        return arr[usedSize - 1];
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean full() {
        if (usedSize == arr.length) {
            return true;
        }
        return false;
    }

    @Override
    public boolean empty() {
        return usedSize == 0;
    }

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String x : tokens) {
            if (isOpration(x)) {
                Integer num2 = stack.pop();
                Integer num1 = stack.pop();
                if (x.equals("+")) {
                    stack.push(num1 + num2);
                } else if (x.equals("-")) {
                    stack.push(num1 - num2);
                } else if (x.equals("*")) {
                    stack.push(num1 * num2);
                } else if (x.equals("/")) {
                    stack.push(num1 / num2);
                }
            } else {
                stack.push(Integer.parseInt(x));
            }
        }
        return stack.pop();
    }

    private boolean isOpration(String x) {
        if (x.equals("+") || x.equals("-") || x.equals("*") || x.equals("/")) {
            return true;
        }
        return false;
    }

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
                stack.push(s.charAt(i));
            } else {
                if (stack.empty()) {
                    return false;
                }
                if (stack.peek() == '(' && s.charAt(i) == ')') {
                    stack.pop();
                } else if (stack.peek() == '[' && s.charAt(i) == ']') {
                    stack.pop();
                } else if (stack.peek() == '{' && s.charAt(i) == '}') {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if (stack.size() != 0) {
            return false;
        }
        return true;
    }

    public boolean IsPopOrder(int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        if (pushV.length != popV.length) {
            return false;
        }
        if(pushV.length==1) {
            if(pushV[0]!=popV[0]) {
                return false;
            }
            return true;
        }
        int countPush = 0;
        int countPop = 0;

        while (countPush<=pushV.length-1) {
            stack.push(pushV[countPush]);
            while (stack.peek()==popV[countPop]&& !stack.empty()) {
                stack.pop();
                countPop++;
            }

            countPush++;
        }
        while (!stack.empty()) {
            if(stack.peek()==popV[countPop]) {
                stack.pop();
                countPop++;
            } else {
                return false;
            }
        }
        return true;
    }
}
class MinStack {
    public static void main(String[] args) {
        MinStack minStack1 = new MinStack();
        minStack1.push(512);
        minStack1.push(-1024);
        minStack1.push(-1024);
        minStack1.push(512);
        minStack1.pop();
        System.out.println(minStack1.getMin());
        minStack1.pop();
        System.out.println(minStack1.getMin());
        minStack1.pop();
        System.out.println(minStack1.getMin());
    }
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if (minStack.empty()) {
            minStack.push(val);
        } else if (val <= minStack.peek()) {
            minStack.push(val);
        }
    }

    public void pop() {
        if (!minStack.empty() && stack.peek() == minStack.peek()) {
            minStack.pop();
        }
        stack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        if (!minStack.empty()) {
            return minStack.peek();
        } else {
            return 0;
        }
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */